package com.pinyoumei.app.activity.wallet;

import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import com.lzy.okgo.OkGo;
import com.lzy.okgo.callback.StringCallback;
import com.lzy.okgo.model.Response;
import com.lzy.okgo.request.base.Request;
import com.pinyoumei.app.R;
import com.pinyoumei.app.activity.BaseActivityCurrency;
import com.pinyoumei.app.bean.BaseBean;
import com.pinyoumei.app.network.Constants;
import com.pinyoumei.app.utils.SPUtils;
import com.pinyoumei.app.utils.SignatureUtils;
import com.pinyoumei.app.utils.ToastUtils;
import com.pinyoumei.app.utils.Utils;

import java.util.HashMap;
import java.util.Map;

public class WalletHappyGroupActivity extends BaseActivityCurrency {
    private TextView tvHappyNum;
    private EditText tvHappyInut;

    @Override
    protected int layoutId() {
        return R.layout.wallet_happy_group_view;
    }

    @Override
    protected void initView() {
        tvHappyNum = findViewById(R.id.tv_happy_num);
        tvHappyInut = findViewById(R.id.tv_happy_inut);
        TextView inlTitle = findViewById(R.id.include_title);
        inlTitle.setText("转开心团券");
        findViewById(R.id.tv_happy_all).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                tvHappyInut.setText(tvHappyNum.getText().toString());
            }
        });
        findViewById(R.id.wggv_substr).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                userBalanceToHappyCouponSubmit();
            }
        });
        findViewById(R.id.include_back).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
    }

    @Override
    protected void initData() {
        userBalanceToHappyCouponInit(SPUtils.getAppUserId(this));
    }

    public void userBalanceToHappyCouponInit(String user_id){
        Map<String, Object> map = new HashMap<>();
        map.put("user_id",user_id);
        OkGo.<String>post(Constants.userBalanceToHappyCouponInit)
                .params("user_id", user_id)
                .params("sign", SignatureUtils.createSignature(map, Constants.SIGN_SECRET))
                .execute(new StringCallback() {

                    @Override
                    public void onStart(Request<String, ? extends Request> request) {
                        super.onStart(request);
                        loadingDailog.show();
                    }

                    @Override
                    public void onSuccess(Response<String> response) {
                        loadingDailog.dismiss();
                        UbtgcBean ubtgcBean = gson.fromJson(response.body(), UbtgcBean.class);
                        tvHappyNum.setText(ubtgcBean.getResult().getUser_balance());
                    }

                    @Override
                    public void onError(Response<String> response) {
                        super.onError(response);
                        loadingDailog.dismiss();
                    }
                });
    }

    public void userBalanceToHappyCouponSubmit(){
        Map<String, Object> map = new HashMap<>();
        map.put("user_id",SPUtils.getAppUserId(this));
        map.put("transfer_amount",tvHappyInut.getText().toString());
        OkGo.<String>post(Constants.userBalanceToHappyCouponSubmit)
                .params("user_id", SPUtils.getAppUserId(this))
                .params("transfer_amount", tvHappyInut.getText().toString())
                .params("sign", SignatureUtils.createSignature(map, Constants.SIGN_SECRET))
                .execute(new StringCallback() {

                    @Override
                    public void onStart(Request<String, ? extends Request> request) {
                        super.onStart(request);
                        loadingDailog.show();
                    }

                    @Override
                    public void onSuccess(Response<String> response) {
                        loadingDailog.dismiss();
                        BaseBean ubtgcBean = gson.fromJson(response.body(), BaseBean.class);
                        ToastUtils.showLong(ubtgcBean.getMsg());
                        tvHappyInut.setText("");
                        userBalanceToHappyCouponInit(SPUtils.getAppUserId(WalletHappyGroupActivity.this));
                    }

                    @Override
                    public void onError(Response<String> response) {
                        super.onError(response);
                        loadingDailog.dismiss();
                    }
                });
    }
}
